#!/bin/bash

mcEngine="TGeant3"

echo "Supported data formats: binary, boost, boost-text, flatbuffers, msgpack, protobuf, tmessage"
dataFormat="binary"
if [ "$1" = "binary" ]; then
    echo "Using: binary"
elif [ "$1" = "boost" ]; then
    dataFormat="boost"
    echo "Using: boost (Boost binary)"
elif [ "$1" = "flatbuffers" ]; then
    if(@FLATBUFFERS_USED@); then
        dataFormat="flatbuffers"
        echo "Using: flatbuffers (Google FlatBuffers)"
    else
        echo "Cannot use flatbuffers: library not found at build time"
        exit 1
    fi
elif [ "$1" = "msgpack" ]; then
    if(@MSGPACK_USED@); then
        dataFormat="msgpack"
        echo "Using: msgpack (MessagePack)"
    else
        echo "Cannot use msgpack: library not found at build time"
        exit 1
    fi
elif [ "$1" = "protobuf" ]; then
    if(@PROTOBUF_USED@); then
        dataFormat="protobuf"
        echo "Using: protobuf (Google Protocol Buffers)"
    else
        echo "Cannot use protobuf: library not found at build time"
        exit 1
    fi
elif [ "$1" = "tmessage" ]; then
    dataFormat="tmessage"
    echo "Using: tmessage (Root TMessage)"
else
    echo "None or incorrect data format provided!"
    echo "Using: binary"
fi

SAMPLER="tut3-sampler"
SAMPLER+=" --id sampler1"
SAMPLER+=" --data-format $dataFormat"
SAMPLER+=" --severity info"
SAMPLER+=" --chain-input 99"
SAMPLER+=" --input-file @CMAKE_SOURCE_DIR@/examples/advanced/Tutorial3/macro/data/testdigi_$mcEngine.root"
SAMPLER+=" --parameter-file @CMAKE_SOURCE_DIR@/examples/advanced/Tutorial3/macro/data/testparams_$mcEngine.root"
SAMPLER+=" --channel-config name=data1,type=push,method=bind,address=tcp://localhost:5565"
SAMPLER+="                  name=ack,type=pull,method=bind,address=tcp://localhost:5999"
xterm -geometry 80x23+0+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$SAMPLER &

PROCESSOR="tut3-processor"
PROCESSOR+=" --id processor1"
PROCESSOR+=" --data-format $dataFormat"
PROCESSOR+=" --severity info"
PROCESSOR+=" --channel-config name=data1,type=pull,method=connect,address=tcp://localhost:5565"
PROCESSOR+="                  name=data2,type=push,method=connect,address=tcp://localhost:5570"
xterm -geometry 80x23+500+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$PROCESSOR &

FILESINK="tut3-sink"
FILESINK+=" --id sink1"
FILESINK+=" --data-format $dataFormat"
FILESINK+=" --severity info"
FILESINK+=" --channel-config name=data2,type=pull,method=bind,address=tcp://localhost:5570"
FILESINK+="                  name=ack,type=push,method=connect,address=tcp://localhost:5999"
xterm -geometry 80x23+1000+0 -hold -e @CMAKE_BINARY_DIR@/bin/examples/advanced/Tutorial3/$FILESINK &